//
//  WXPTwoBtnPopUpWindowView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/8.
//

#import "WXPTwoBtnPopUpWindowView.h"

@implementation WXPTwoBtnPopUpWindowView

- (UIView *)viewBack{
    if (_viewBack == nil) {
        _viewBack = [UIView new];
        _viewBack.userInteractionEnabled = true;
        _viewBack.backgroundColor = COLOR_WHITE;
        _viewBack.width = SCREEN_WIDTH-2*LEFT_MARDIN;
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:16 width:0];
    }
    return _viewBack;
}
- (UIImageView *)imageLogo{
    if (!_imageLogo) {
        _imageLogo = [UIImageView new];
    }
    return _imageLogo;
}
- (UILabel *)labelTitle{
    if (_labelTitle == nil) {
        _labelTitle = [UILabel new];
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.textAlignment = NSTextAlignmentCenter;
        _labelTitle.fontNum = 18;
        _labelTitle.numberOfLines = 0;
        _labelTitle.lineSpace = 3;
    }
    return _labelTitle;
}
- (UILabel *)labelContent{
    if (_labelContent == nil) {
        _labelContent = [UILabel new];
        _labelContent.textColor = COLOR_CONTENT;
        _labelContent.textAlignment = NSTextAlignmentCenter;
        _labelContent.font = [UIFont boldSystemFontOfSize:12];
        _labelContent.numberOfLines = 0;
        _labelContent.lineSpace = 3;
    }
    return _labelContent;
}
- (UIButton *)btnClose{
    if (_btnClose == nil) {
        _btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnClose.tag = 1;
        [_btnClose addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_btnClose setImage:[UIImage imageNamed:@"icon-login-close"] forState: UIControlStateNormal];
        _btnClose.widthHeight = XY(44, 44);
    }
    return _btnClose;
}
- (UIButton *)btnLeft{
    if (_btnLeft == nil) {
        _btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnLeft.tag = 2;
        [_btnLeft addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _btnLeft.backgroundColor = COLOR_PRIMARY;
        _btnLeft.alpha = 0.5;
        _btnLeft.titleLabel.font = [UIFont systemFontOfSize:14];
        _btnLeft.widthHeight = XY((self.viewBack.width-30-30-20)/2, BUTTON_HEIGHT);
        [GlobalMethod setRoundView:_btnLeft color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
        [_btnLeft setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [_btnLeft setTitle:@"cancelar" forState:(UIControlStateNormal)];
    }
    return _btnLeft;
}
- (UIButton *)btnRight{
    if (_btnRight == nil) {
        _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnRight.tag = 3;
        [_btnRight addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        _btnRight.backgroundColor = COLOR_PRIMARY;
        _btnRight.titleLabel.font = [UIFont systemFontOfSize:14];
        _btnRight.widthHeight = self.btnLeft.widthHeight;
        [GlobalMethod setRoundView:_btnRight color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
        [_btnRight setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [_btnRight setTitle:@"verificar" forState:(UIControlStateNormal)];
    }
    return _btnRight;
}

#pragma mark 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.4];
        self.userInteractionEnabled = true;
        [self addSubView];
    }
    return self;
}
//添加subview
- (void)addSubView{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.imageLogo];
    [self.viewBack addSubview:self.labelTitle];
    [self.viewBack addSubview:self.labelContent];
    [self.viewBack addSubview:self.btnClose];
    [self.viewBack addSubview:self.btnLeft];
    [self.viewBack addSubview:self.btnRight];
}

#pragma mark 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    [sender addClickAnimate];
    switch (sender.tag) {
        case 1:
        {
            [self removeFromSuperview];
        }
            break;
        case 2:
        {
            if(self.blockLeftClick){
                self.blockLeftClick();
            }else{
                [self removeFromSuperview];
            }
        }
            break;
        case 3:
        {
            if(self.blockRightClick){
                self.blockRightClick();
            }else{
                [self removeFromSuperview];
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark 刷新view
- (void)resetWithModel:(ModelBtn *)model{
    //刷新view
    self.btnClose.rightTop = XY(self.viewBack.width, 0);
    self.btnClose.hidden = model.isHide;
    
    self.imageLogo.hidden = !isStr(model.imageName);
    self.imageLogo.width = model.width > 0 ? model.width : 30;
    self.imageLogo.height = model.height > 0 ? model.height : 30;
    if(isStr(model.imageName)){
        self.imageLogo.image = [UIImage imageNamed:model.imageName];
    }
    self.imageLogo.centerXTop = XY(self.viewBack.width/2, 40);
    
    self.labelTitle.hidden = !isStr(model.title);
    [self.labelTitle fitTitle:readStr(model.title) variable:self.viewBack.width-40-40];
    self.labelTitle.centerXTop = XY(self.viewBack.width/2, 40);
    
    self.labelContent.hidden = !isStr(model.content);
    [self.labelContent fitTitle:readStr(model.content) variable:self.viewBack.width-30-30];
    self.labelContent.centerXTop = XY(self.viewBack.width/2, isStr(model.title) ? (self.labelTitle.bottom+40) : self.imageLogo.hidden == false ? (self.imageLogo.bottom+34) : 40);
    
    if(isStr(model.leftName)){
        [self.btnLeft setTitle:model.leftName forState:UIControlStateNormal];
    }
    self.btnLeft.leftTop = XY(30, self.labelContent.hidden == false ? (self.labelContent.bottom+30) : self.labelContent.top);
    
    if(isStr(model.rightName)){
        [self.btnRight setTitle:model.rightName forState:UIControlStateNormal];
    }
    self.btnRight.rightCenterY = XY(self.viewBack.width-self.btnLeft.left, self.btnLeft.centerY);
    
    self.viewBack.height = self.btnLeft.bottom+40;
    self.viewBack.centerXCenterY = XY(self.width/2, self.height/2);
}

- (void)resetOnlyShowRightBtn{
    self.btnLeft.hidden = true;
    self.btnRight.width = self.viewBack.width-30-30;
    self.btnRight.centerX = self.viewBack.width/2;
}



@end
